package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.HealthManager;
import com.itheima.pojo.Member;
import com.itheima.service.CheckItemService;
import com.itheima.service.MemberService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {


    @Reference
    private MemberService memberService;

    //分页查询
    @RequestMapping("/findByPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pg = memberService.pageQuery(queryPageBean);
        return pg;
    }

    @RequestMapping("/findById")
    public Result findById(int id) {
        Member member = null;
        try {
            member = memberService.findById(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, member);
    }

    //新增和修改
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member) {
        if(member.getId()!=null){
            try {
                memberService.edit(member);
            } catch (Exception e) {
                return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
            }
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        }else{
            try {
                memberService.add(member);
            } catch (Exception e) {
                return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
            }
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        }
    }
    //删除
    @RequestMapping("/delete")
    public Result delete(Integer[] id) {
        int count = memberService.findMidByMemberId(id);
        if(count>0){
            return new Result(false, "该会员已经录入体检表，不能删除！");
        }
        boolean delete = false;
        try {
            delete = memberService.deletes(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(delete){
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }else{
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    @RequestMapping("/findAllHealthManager")
    public Result findAllHealthManager(){
        List<HealthManager> list = memberService.findAllHealthManager();
        if(list!=null && list.size()>0){
            return new Result(true,"查询成功",list);
        }
        return new Result(false,"查询失败");
    }
}
